package com.yami.shop.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.enums.SendType;
import com.yami.shop.bean.event.SendPersonMessageEvent;
import com.yami.shop.bean.model.*;
import com.yami.shop.bean.param.NotifyTemplateParam;
import com.yami.shop.bean.vo.ReportDetailVO;
import com.yami.shop.common.config.Constant;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.i18n.I18nMessage;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.common.util.StringUtils;
import com.yami.shop.security.api.util.SecurityUtils;
import com.yami.shop.service.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.context.ApplicationContext;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/p/report")
@AllArgsConstructor
@Tag(name = "举报")
public class ReportController {

    private final ReportService reportService;

    private final ReportTitleService reportTitleService;

    private final BlogService blogService;

    private final CommentService commentService;

    private final ApplicationContext applicationContext;

    private final UserService userService;

    private final MeltCloudUserRemarkService userRemarkService;

    private final MeltCloudGroupService meltCloudGroupService;


    @GetMapping("/title/list")
    @Operation(summary =  "举报标题列表", description = "举报标题列表")
    public ServerResponseEntity<List<ReportTitle>> reportTitleList(@RequestParam("type") Integer type) {
        List<ReportTitle> list = reportTitleService.list(new LambdaQueryWrapper<ReportTitle>().eq(ReportTitle::getType, type).orderByAsc(ReportTitle::getOrderNum));
        return ServerResponseEntity.success(list);
    }

    @PostMapping
    @Operation(summary =  "举报", description = "举报")
    @Transactional
    public ServerResponseEntity addReport(@RequestBody Report report) {
        String userId = SecurityUtils.getUser().getUserId();
        report.setUserId(userId);
        report.setStatus(0);
        if (Objects.isNull(report.getTitleId())) {
            return ServerResponseEntity.showFailMsg(I18nMessage.getMessage("yami.dtc.report.check.reason"));
        }
        String content = "";
        if (Objects.nonNull(report.getReportSou()) && report.getReportSou().equals(2)) {
            //聊天举报
            if (StringUtils.isEmpty(report.getReportPics())) {
                return ServerResponseEntity.showFailMsg(I18nMessage.getMessage("yami.dtc.report.check.evidence"));
            }
            if (report.getReportType().equals(2) && StringUtils.isEmpty(report.getReportUserId())) {
                return ServerResponseEntity.showFailMsg(I18nMessage.getMessage("yami.dtc.report.check.people"));
            }
            if (report.getReportType().equals(3) && Objects.isNull(report.getReportObjId())) {
                return ServerResponseEntity.showFailMsg(I18nMessage.getMessage("yami.dtc.report.check.groupchat"));
            }
        } else {
            if (Objects.isNull(report.getReportObjId())) {
                return ServerResponseEntity.showFailMsg(I18nMessage.getMessage("yami.dtc.report.check.target"));
            }
            if (report.getReportType().equals(0)) {
                Blog blog = blogService.getById(report.getReportObjId());
                content = blog.getType().equals(0) ? blog.getTitle() : blog.getType().equals(2) ? blog.getContent() : "";
                report.setReportSou(blog.getType() == 0 ? 0 : 1);
                report.setReportUserId(blog.getUserUid());
                if (blog.getArticleSource().equals(0)) {
                    report.setReportShopId(blog.getShopId());
                } else {
                    if (blog.getArticleSource().equals(2)) {
                        report.setReportShopId(0L);
                    }
                }
            } else if (report.getReportType().equals(1)) {
                Comment comment = commentService.getById(report.getReportObjId());
                report.setReportUserId(comment.getUserUid());
                report.setReportSou(comment.getSource().equals("1") ? 0 : 1);
                content = comment.getContent();
            } else {
                return ServerResponseEntity.showFailMsg(I18nMessage.getMessage("yami.dtc.report.check.type"));
            }
        }
        reportService.save(report);
        ReportTitle reportTitle = reportTitleService.getById(report.getTitleId());
        String title = reportTitle.getTitle();
        NotifyTemplateParam shopParam = new NotifyTemplateParam();
        shopParam.setShopId(Constant.PLATFORM_SHOP_ID);
        shopParam.setSendType(SendType.REPORT_REMINDER.getValue());
        shopParam.setUserId(userId);
        shopParam.setTitle(title);
        if (report.getReportSou().equals(2)) {
            if (report.getReportType().equals(3)) {
                MeltCloudGroup cloudGroup = meltCloudGroupService.getById(report.getReportObjId());
                String groupName = cloudGroup.getGroupName();
                MeltCloudUserRemark remark = userRemarkService.getRemark(2, userId, report.getReportObjId().toString());
                if (Objects.nonNull(remark) && StringUtils.isNotEmpty(remark.getRemark())) {
                    groupName = remark.getRemark();
                }
                content = "[" + groupName + "]群聊";
            } else {
                User user = userService.getUserByUserId(report.getReportUserId());
                String nickName = user.getNickName();
                MeltCloudUserRemark remark = userRemarkService.getRemark(1, userId, report.getReportUserId());
                if (Objects.nonNull(remark) && StringUtils.isNotEmpty(remark.getRemark())) {
                    nickName = remark.getRemark();
                }
                content = "与" + nickName + "的聊天";
            }
        } else {
            content = content.length() > 14 ? content.substring(0, 14) + "…" : content;
        }
        shopParam.setContent(content);
        applicationContext.publishEvent(new SendPersonMessageEvent(shopParam));
        return ServerResponseEntity.success();
    }


    @GetMapping("/page")
    @Operation(summary =  "举报标题列表", description = "举报标题列表")
    public ServerResponseEntity<IPage<ReportDetailVO>> page(PageParam<Report> page, @ParameterObject ReportDetailVO param) {
        String userId = SecurityUtils.getUser().getUserId();
        param.setUserId(userId);
        IPage<ReportDetailVO> iPage = reportService.page(page, param);
        return ServerResponseEntity.success(iPage);
    }











}
